package com.lhn.tankGame4;

import java.util.Random;
import java.util.Vector;

/**
 * @author lhn
 * @date 2024-07-28 13:32
 */

public class EnemyTank extends Tank implements Runnable{

    //使用Vector保存多个shoot
    Vector<Shoot> shoots = new Vector<>();
    //Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();

    boolean isLive = true;
    public EnemyTank(int x, int y, int direction) {
        super(x, y, direction);
    }

    public EnemyTank(int x, int y, int direction, int speed) {
        super(x, y, direction, speed);
    }

    public Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    //编写方法，判断当前这个敌人坦克是否和enemyTanks中的其他坦克发生的重叠或者是碰撞
    public boolean isTouchEnemyTank() {

        //判断当前的坦克方向
        switch (this.getDirection()) {
            case 0:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);

                    //另一辆坦克
                    if (enemyTank != this) {
                        if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2) {
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3) {
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {
                        if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2) {
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3) {
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {
                        if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2) {
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3) {
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {
                        if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2) {
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3) {
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }

    //射击
    public void shootEnemy() {
        Shoot shoot = null;
        switch (
                getDirection()
        ) {
            case 0:
                shoot = new Shoot(getX() + 20, getY(), getDirection());
                break;
            case 1:
                shoot = new Shoot(getX() + 60, getY() + 20, getDirection());
                break;
            case 2:
                shoot = new Shoot(getX() + 20, getY() + 60, getDirection());
                break;
            case 3:
                shoot = new Shoot(getX() + 60, getY() + 20, getDirection());
                break;
        }
        shoots.add(shoot);

        Thread thread = new Thread(shoot);
        thread.start();
    }

    @Override
    public void run() {
        while(isLive) {

            //每个敌军坦克只能存在一颗子弹
//            if (isLive && shoots.size() == 0) {
//                shootEnemy();
//            }

            //等待随机时间后发射子弹
            try {
                Thread.sleep(1000 * new Random().nextInt(6));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            shootEnemy();

            //根据坦克的方向继续移动
            int s = new Random().nextInt(30);
            for (int i = 0; i < s; i++) {
                switch (getDirection()) {
                    case 0:
//                        if (getY() - getSpeed() >= 0 && !isTouchEnemyTank()) {
                        if (getY() >= 0 && !isTouchEnemyTank()) {
                            moveUp();
                        }
                        break;
                    case 1:
//                        if (getX() + getSpeed() + 60 <= 1000  && !isTouchEnemyTank()) {
                        if (getX() + 60 <= 1000  && !isTouchEnemyTank()) {
                            moveRight();
                        }
                        break;
                    case 2:
//                        if (getY() + getSpeed() + 60 <= 750  && !isTouchEnemyTank()) {
                        if (getY() + 60 <= 750  && !isTouchEnemyTank()) {
                            moveDown();
                        }
                        break;
                    case 3:
//                        if (getX() - getSpeed() >= 0  && !isTouchEnemyTank()) {
                        if (getX() >= 0  && !isTouchEnemyTank()) {
                            moveLeft();
                        }
                        break;
                }
                try {
                    Thread.sleep((250));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            setDirection((new Random().nextInt(4)));

//            System.out.println(
//                    "当前线程名：" + Thread.currentThread().getName() + "  " + Thread.currentThread().getId()
//            );
        }
    }
}
